package com.zzh.bc.service.api.impl;

import com.zzh.bc.dao.api.ApiDao;
import com.zzh.bc.dao.api.ApiParamDao;
import com.zzh.bc.dao.api.impl.ApiDaoImpl;
import com.zzh.bc.dao.api.impl.ApiParamDaoImpl;
import com.zzh.bc.dao.project.ProjectDao;
import com.zzh.bc.dao.project.impl.ProjectDaoImpl;
import com.zzh.bc.model.ApiModel;
import com.zzh.bc.model.ApiParamModel;
import com.zzh.bc.model.PageModel;
import com.zzh.bc.modules.api.vo.ApiVo;
import com.zzh.bc.service.api.ProjectApiService;
import org.apache.commons.lang.StringUtils;

import java.util.List;
import java.util.UUID;

/**
 * 保存接口业务处理
 */
public class ProjectApiServiceImpl implements ProjectApiService {
    ProjectDao dao;
    ApiParamDao apiParamDao;
    ApiDao apiDao;


    @Override
    public boolean saveOrUpdate(ApiVo vo) {
        if (StringUtils.isNotEmpty(vo.getApiId())) {
            return update(vo);
        } else {
            return save(vo);
        }
    }

    /**
     * @param vo
     * @return
     */
    @Override
    public boolean save(ApiVo vo) {
        checkDao();
        apiDao.beginTransaction();
        ApiModel model = new ApiModel();
        String pk = UUID.randomUUID().toString();
        model.setApiId(pk);
        model.setApiName(vo.getApiName());
        model.setApiPattern(vo.getApiPattern());
        model.setApiProjectId(vo.getApiProjectId());
        model.setApiReqMethod(vo.getApiReqMethod());
        model.setCreatedBy(vo.getCreatedBy());
        model.setUpdatedBy(vo.getUpdatedBy());
        model.setCreatedTime(System.currentTimeMillis());
        model.setUpdatedTime(System.currentTimeMillis());
        apiDao.save(model);
        if (vo.getData() != null && !vo.getData().isEmpty()) {
            vo.getData().forEach(child -> {
                ApiParamModel sub = new ApiParamModel();
                sub.setApiParamId(child.getApiParamId());
                sub.setApiParamKey(child.getApiParamKey());
                sub.setApiParamName(child.getApiParamName());
                sub.setApiParamsDetails(child.getApiParamsDetails());
                sub.setApiParamValue(child.getApiParamValue());
                sub.setApiProjectId(pk);
                sub.setApiReqMethodId(child.getApiReqMethodId());
                sub.setCreatedBy(child.getCreatedBy());
                sub.setCreatedTime(System.currentTimeMillis());
                sub.setUpdatedBy(child.getUpdatedBy());
                sub.setUpdatedTime(sub.getCreatedTime());
                apiParamDao.save(sub);
            });
        }
        apiDao.commitTransaction();
        return true;
    }

    @Override
    public boolean update(ApiVo vo) {
        checkDao();
        apiDao.beginTransaction();
        ApiModel model = findObjById(vo.getApiId());
        model.setApiName(vo.getApiName());
        model.setApiPattern(vo.getApiPattern());
        model.setApiProjectId(vo.getApiProjectId());
        model.setApiReqMethod(vo.getApiReqMethod());
        model.setCreatedBy(vo.getCreatedBy());
        model.setUpdatedBy(vo.getUpdatedBy());
        model.setUpdatedTime(System.currentTimeMillis());
        apiDao.update(model);
        apiParamDao.delete(vo.getApiId());
        if (vo.getData() != null && !vo.getData().isEmpty()) {
            vo.getData().forEach(child -> {
                ApiParamModel sub = new ApiParamModel();
                sub.setApiParamId(child.getApiParamId());
                sub.setApiParamKey(child.getApiParamKey());
                sub.setApiParamName(child.getApiParamName());
                sub.setApiParamsDetails(child.getApiParamsDetails());
                sub.setApiParamValue(child.getApiParamValue());
                sub.setApiProjectId(vo.getApiId());
                sub.setApiReqMethodId(child.getApiReqMethodId());
                sub.setCreatedBy(child.getCreatedBy());
                sub.setCreatedTime(System.currentTimeMillis());
                sub.setUpdatedBy(child.getUpdatedBy());
                sub.setUpdatedTime(sub.getCreatedTime());
                apiParamDao.update(sub);
            });
        }
        apiDao.commitTransaction();
        return true;
    }

    @Override
    public ApiModel findObjById(String id) {
        checkDao();
        ApiModel apiModel = apiDao.findObjById(id);
        return apiModel;
    }

    @Override
    public boolean delete(String apiId) {
        checkDao();

        ApiModel model = findObjById(apiId);
        if (model != null) {
            apiDao.beginTransaction();
            if (model.getData() != null && !model.getData().isEmpty()) {
                model.getData().forEach(child -> {
                    apiParamDao.delete(child.getApiParamId());
                });
            }
            boolean delete = apiDao.delete(apiId);
            apiDao.commitTransaction();
            return delete;
        } else {
            return true;
        }
    }

    @Override
    public boolean deleteParams(String ids) {
        if (StringUtils.isNotEmpty(ids)) {
            String[] split = StringUtils.split(ids, ",");
            if (split != null && split.length > 0) {
                for (String id : split) {
                    apiParamDao.delete(id);
                }
            }
        }
        return true;
    }

    void checkDao() {
        if (dao == null) {
            dao = new ProjectDaoImpl();
        }
        if (apiParamDao == null) {
            apiParamDao = new ApiParamDaoImpl();
        }
        if (apiDao == null) {
            apiDao = new ApiDaoImpl();
        }
    }
}
